/*
 * Copyright (C) 2008 ProSyst Software GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.modulefusion.example.wicket.ipojo.crud.person;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.modulefusion.example.jpa.personprovider.Person;
import org.modulefusion.example.jpa.personprovider.PersonService;
import org.modulefusion.example.wicket.ipojo.crud.WicketCrudApplication;

public class ListPersons extends WebPage<Object> {

	private ModalWindow modal;
    private PersonService personService;

	public ListPersons() {
        personService =  ((WicketCrudApplication) getApplication()).getPersonService();

		List<Person> persons = personService.getPersonList();
		ListView<Person> personListView = new ListView<Person>("person", persons) {
			@Override
			protected void populateItem(final ListItem<Person> item) {
				item.setOutputMarkupId(true);
				
				final Person person = item.getModelObject();
				item.setModel(new CompoundPropertyModel<Person>(person));

				item.add(new Label<String>("firstname"));
				item.add(new Label<String>("lastname"));

				Link<Object> details = new Link<Object>("details") {
					@Override
					public void onClick() {
						PersonDetail pd = new PersonDetail(person.getId());
						setResponsePage(pd);
					}
				};
				item.add(details);

				Link<Object> edit = new Link<Object>("edit") {
					@Override
					public void onClick() {
						EditPerson ep = new EditPerson(person.getId());
						setResponsePage(ep);
					}
				};
				item.add(edit);

				AjaxLink<Object> delete = new AjaxLink<Object>("delete") {
					public void onClick(AjaxRequestTarget target) {
						DeletePerson deletePanel = new DeletePerson(modal.getContentId(), modal, person) {
							protected void onYes(AjaxRequestTarget target) {
								item.setVisible(false);
								target.addComponent(item);
							}
						};
						modal.setContent(deletePanel);
						modal.show(target);
					}
				};
				item.add(delete);
			}
		};
		add(personListView);
		
		modal = new ModalWindow("modal");
		add(modal);

	}
}
